package com.woniu.car.controller;


import com.alipay.api.domain.Coupon;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.entity.TCoupon;
import com.woniu.car.exception.StatisticsException;
import com.woniu.car.feign.PlatformClient2Account;
import com.woniu.car.parameter.CouponParameter;
import com.woniu.car.parameter.CouponSearchParameter;
import com.woniu.car.parameter.TCouponToUserParameter;
import com.woniu.car.properties.StatisticsProperties;
import com.woniu.car.service.TCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-06-22
 */
@RestController
@Api(tags = "优惠券基础控制层")
public class TCouponController {
    @Autowired
    private TCouponService couponService;
    @Resource
    private PlatformClient2Account client;
    @PostMapping("/insertCoupon")
    @ApiOperation(value = "新增优惠券")
    public ResponseEntity insertCoupon(@Validated CouponParameter parameter,@RequestHeader("id") int id) throws Exception {
        couponService.insertCoupon(id,parameter);
        return new ResponseEntity().setCode(StatisticsProperties.SUCCESSCODE)
                                   .setMsg(StatisticsProperties.SUCCESSMSG);
    }
    
//    @DeleteMapping("/deleteCoupon")
//    @ApiModelProperty(value="逻辑删除优惠券")
//    public ResponseEntity deleteCoupon(Integer couponId) throws StatisticsException {
//        couponService.deleteCoupon(couponId);
//        return new ResponseEntity().setCode(StatisticsProperties.SUCCESSCODE)
//                                   .setMsg(StatisticsProperties.SUCCESSMSG);
//    }
   
    @GetMapping("/searchCoupon")
    @ApiOperation(value="查询优惠券")
    public ResponseEntity searchCoupon(@Validated CouponSearchParameter parameter) throws Exception {
        TCoupon coupon = couponService.searchCoupon(parameter);
        if(null==coupon)
            throw new StatisticsException("无优惠券信息");
        return new ResponseEntity().setCode(StatisticsProperties.SUCCESSCODE)
                                   .setMsg(StatisticsProperties.SUCCESSMSG)
                                   .setObject(coupon);
    }

    @GetMapping("/searchCouponByCouponId")
    @ApiOperation(value="查询优惠券")
    public ResponseEntity searchCoupon(int couponId) throws Exception {
        TCoupon coupon = couponService.getOne(new QueryWrapper<TCoupon>().eq("coupon_id",couponId));
        if(null==coupon)
            throw new StatisticsException("无优惠券信息");
        coupon.setUsedCount(coupon.getUsedCount()+1);
        return new ResponseEntity().setCode(StatisticsProperties.SUCCESSCODE)
                .setMsg(StatisticsProperties.SUCCESSMSG)
                .setObject(coupon);
    }
//    @GetMapping("/giveCouponToUser")
//    @ApiOperation("发送优惠券")
//    public ResponseEntity giveCouponToUser(@Validated TCouponToUserParameter tCouponToUserParameter)throws Exception{
//        TCoupon coupon = couponService.getOne(new QueryWrapper<TCoupon>().eq("coupon_id", tCouponToUserParameter.getCouponId()));
//        if(null==coupon)
//            throw new StatisticsException("无此优惠券信息");
//        System.out.println(coupon.getTakeCount());
//        System.out.println(coupon.getUsedCount());
//        System.out.println(tCouponToUserParameter.getTakeNum());
//        if(coupon.getTakeCount()-coupon.getUsedCount()<=tCouponToUserParameter.getTakeNum())
//            throw new StatisticsException("优惠券数量不足无法发放");
//        ResponseEntity responseEntity = client.queryUserById(tCouponToUserParameter.getUserId());
//        System.out.println(responseEntity.getObject());
//        if(null==responseEntity.getObject())
//            throw new StatisticsException("此用户不存在无法发送优惠券");
//        coupon.setUsedCount(coupon.getUsedCount() + tCouponToUserParameter.getTakeNum());
//        couponService.updateById(coupon);
//        return new ResponseEntity().setCode(StatisticsProperties.SUCCESSCODE)
//                                    .setMsg(StatisticsProperties.SUCCESSMSG);
//    }
}

